﻿using Common.Models;
using Core.Entities.EQ;
using Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Infrastructure.Services.EQ
{
    /// <summary>
    /// 设备状态报警信息服务
    /// </summary>
    public class DevStatusService : BaseService<DevStatus, string>
    {
        private readonly IDapperRepository _dapperRepository;

        public DevStatusService(
            IBaseRepository<DevStatus, string> repository,
            IPagedRepository<DevStatus, string> pagedRepository,
            IDapperRepository dapperRepository)
            : base(repository, pagedRepository)
        {
            _dapperRepository = dapperRepository;
        }

        /// <summary>
        /// 根据设备编码查询
        /// </summary>
        /// <param name="devCode">设备编码</param>
        /// <returns>设备状态报警信息列表</returns>
        public async Task<IEnumerable<DevStatus>> GetByDevCodeAsync(string devCode)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "DevCode", Operator = "=", Value = devCode }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据设备名称模糊查询
        /// </summary>
        /// <param name="devName">设备名称</param>
        /// <returns>设备状态报警信息列表</returns>
        public async Task<IEnumerable<DevStatus>> GetByDevNameAsync(string devName)
        {
            var filter = new QueryFilter
            {
                Conditions = new List<FilterCondition>
                {
                    new FilterCondition { Field = "DevName", Operator = "like", Value = devName }
                }
            };

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result.Data;
        }

        /// <summary>
        /// 根据设备状态报警信息查询
        /// </summary>
        /// <returns>设备状态报警信息列表</returns>
        public async Task<PagedResult<DevStatus>> GetByDevStatusAsync(QueryFilter filter, DevStatus devStatus)
        {
            // 初始化条件列表
            filter.Conditions = new List<FilterCondition>();

            // 添加设备编码模糊查询条件
            if (!string.IsNullOrEmpty(devStatus.DevCode))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevCode",
                    Operator = "like",
                    Value = devStatus.DevCode
                });
            }

            // 添加设备名称模糊查询条件
            if (!string.IsNullOrEmpty(devStatus.DevName))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevName",
                    Operator = "like",
                    Value = devStatus.DevName
                });
            }

            // 添加设备状态查询条件
            if (!string.IsNullOrEmpty(devStatus.DevStatusValue))
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "DevStatusValue",
                    Operator = "=",
                    Value = devStatus.DevStatusValue
                });
            }

            // 添加时间范围查询条件
            if (devStatus.CreateTime.HasValue)
            {
                filter.Conditions.Add(new FilterCondition
                {
                    Field = "CreateTime",
                    Operator = ">=",
                    Value = devStatus.CreateTime.Value.ToString("yyyy-MM-dd")
                });
            }

            // 始终添加未删除的条件
            filter.Conditions.Add(new FilterCondition
            {
                Field = "IsDeleted",
                Operator = "=",
                Value = false
            });

            var result = await _pagedRepository.GetPagedAsync(filter);
            return result;
        }
    }
}